/**
 * Copyright &copy; 2012-2016
 * <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights
 * reserved.
 */
package com.thinkgem.jeesite.modules.shop.web.order;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.KDniao.service.logistics.LogisticsCodeComment;
import com.thinkgem.jeesite.modules.config.entity.weixin.ConfigWechat;
import com.thinkgem.jeesite.modules.config.service.weixin.ConfigWechatService;
import com.thinkgem.jeesite.modules.front.web.refund.wx.WXRefundService;
import com.thinkgem.jeesite.modules.shop.constant.order.ShopOrderConstant;
import com.thinkgem.jeesite.modules.shop.entity.order.ShopOrder;
import com.thinkgem.jeesite.modules.shop.service.order.ShopOrderService;

/**
 * 订单管理Controller
 * 
 * @author JiangYongKang
 * @version 2016-12-19
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/order/manage")
public class OrderManageController extends LogisticsCodeComment {

    @Autowired
    private ShopOrderService shopOrderService;
    @Autowired WXRefundService wxRefundService;

    @Autowired
    ConfigWechatService configWechatService;
    

    @ModelAttribute
    public ShopOrder get(@RequestParam(required = false) String id) {
        ShopOrder entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = shopOrderService.get(id);
        }
        if (entity == null) {
            entity = new ShopOrder();
        }
        return entity;
    }

    @RequiresPermissions("shop:order:manage:view")
    @RequestMapping(value = { "list", "" })
    public String list(ShopOrder shopOrder, HttpServletRequest request,
            HttpServletResponse response, Model model) {
        Page<ShopOrder> page = shopOrderService
                .findPage(new Page<ShopOrder>(request, response), shopOrder);
        model.addAttribute("page", page);
        getlogisticsCodes(model);
        return "modules/shop/order/shopOrderList";
    }

    @RequiresPermissions("shop:order:manage:view")
    @RequestMapping(value = "form")
    public String form(ShopOrder shopOrder, Model model) {
        model.addAttribute("shopOrder", shopOrder);
        return "modules/shop/order/shopOrderForm";
    }
    
    @RequiresPermissions("shop:order:manage:view")
    @RequestMapping(value = "refund")
    public String refund(ShopOrder shopOrder, Model model) {
        model.addAttribute("shopOrder", shopOrder);
        return "modules/shop/order/shopOrderRefund";
    }
    
    @RequiresPermissions("shop:order:manage:view")
    @RequestMapping(value = "agreeReject")
    @ResponseBody
    public Object forRefund(HttpServletRequest req,ShopOrder shopOrder, HttpServletResponse resp, Model model) {
		try {
			ConfigWechat configWechat = configWechatService.getConfigWechat();
			Map<String,String> resultMap = wxRefundService.forRefund(String.valueOf(shopOrder.getTotalAmount()), shopOrder.getCode(),configWechat);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
    
    @RequiresPermissions("shop:order:manage:view")
    @RequestMapping(value = "refundReject")
    public String refundReject(ShopOrder shopOrder, Model model) {
    	shopOrder.setStatus(ShopOrderConstant.ORDER_REFUND_REJECT);
    	shopOrderService.updateOrderForPaySuccess(shopOrder);
    	return "redirect:" + Global.getAdminPath()
        + "/shop/order/manage/?repage";
    }
    
    @RequiresPermissions("shop:order:manage:edit")
    @RequestMapping(value = "save")
    public String save(ShopOrder shopOrder, Model model,
            RedirectAttributes redirectAttributes) {
        shopOrderService.save(shopOrder);
        addMessage(redirectAttributes, "保存购物订单成功");
        return "redirect:" + Global.getAdminPath()
                + "/shop/order/manage/?repage";
    }

    @RequiresPermissions("shop:order:manage:delete")
    @RequestMapping(value = "delete")
    public String delete(ShopOrder shopOrder,
            RedirectAttributes redirectAttributes) {
        shopOrderService.delete(shopOrder);
        addMessage(redirectAttributes, "删除购物订单成功");
        return "redirect:" + Global.getAdminPath()
                + "/shop/order/manage/?repage";
    }

    @RequiresPermissions("shop:order:manage:edit")
    @RequestMapping(value = "getlogistics")
    @ResponseBody
    public Object getlogistics(String id, Model model,
            RedirectAttributes redirectAttributes) {
        return getinfo(id, model, true);
    }
}